//
//  SessionViewController.swift
//  NIMSwiftTest
//
//  Created by Angus on 15/11/24.
//  Copyright © 2015年 Amazing. All rights reserved.
//

import UIKit

class SessionViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet weak var tableView: UITableView!
    
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var contentInputViewBottom: NSLayoutConstraint!
  
    
    override func viewDidLoad() {
        print("view did load")
     
        tableView.delegate = self
        tableView.dataSource = self
        tableView.reloadData()
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillChangeFrame:"), name: UIKeyboardWillChangeFrameNotification, object: nil)
    }
    
 
    //MARK: - Notification
    
    func keyboardWillChangeFrame(notification:NSNotification) {
        if let userInfo:Dictionary = notification.userInfo {
            let value:NSValue? = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue

            let keyboardFrame:CGRect = (value?.CGRectValue())!
            let height:CGFloat = UIScreen.mainScreen().bounds.size.height - keyboardFrame.origin.y
            contentInputViewBottom.constant = height
        }
 
        
    }
    
    @IBAction func sendButtonTapped(sender: AnyObject) {
        
        let message:String? = textField.text
        if message != nil && message?.characters.count > 0 {
            
            if textField.isFirstResponder() {
                textField.resignFirstResponder()
            }
            
            //TODO: SEND MESSAGE
        }
    }
    
    
    //MARK: - UITableViewDataSource, UITableViewDelegate
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
 
        
        return cell;
 
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UIScreen.mainScreen().bounds.size.height/10
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        if textField.isFirstResponder() {
            textField.resignFirstResponder()
        }
    }
    
}
